//用户登录名--用于修改用户时判断
var checkLoginName = "";
var pageSize = 5;

$(function() {
	
	//panel初始化
	$('#userEditPanel').panel({
		border:false
	});
	
	//菜单按钮初始化
	$('#editPersonalInfoBtn').linkbutton({plain:true,iconCls:'icon-user-edit'});
	$('#closePersonalInfoBtn').linkbutton({plain:true,iconCls:'icon-close'});
	
	//添加窗口
	$('#eUserWindow').window({
		closed:true,
		modal:true,
		closable:true,
		minimizable:false,
		maximizable:false,
		collapsible:false,
		resizable:false,
		iconCls:'icon-user',
		width:615,
		height:315
	});
	
	//个人信息编辑菜单用户列表
	$('#eUserDataGrid').datagrid({
		url:'userAjax!loadUserEditData',
		nowrap: false,
		border:false,
		striped: true,
		collapsible:true,
		sortName: 'id',
		singleSelect:true,
		sortOrder: 'desc',
		remoteSort: false,
		idField:'id',			
		columns:[[
		           {field:'id',title:'ID',width:50},
		           {field:'loginName',title:'用户名',width:100},
		           {field:'name',title:'姓名',width:100},
		           {field:'sn',title:'编号',width:100},
		           {field:'sex',title:'性别',width:60},
		           {field:'departName',title:'部门',width:100},
		           {field:'roleNames',title:'角色',width:300},
		           {field:'status',title:'状态',width:60},
		           {field:'locked',title:'锁定',width:60}
				]]
	});
	
	//初始化用户性别
	DICT_INIT(GETID('eUserSex'), 'Sex', '', '1');

	
	////个人信息编辑菜单生日日期框
	$('#eUserBirthday').datebox({
		width:205
	});  
	
});


//打开窗口
function openUserWindow() {
	clearTable();
	$("#eUserDataGrid").datagrid('selectRow', 0);
	var selectedRow = $('#eUserDataGrid').datagrid('getSelected');
	$('#eUserWindow').window({title:'编辑'});
	$('#eSubmitUserButton').val('编辑');
	//控件赋值
	$('#eUserName').val(selectedRow.name);
	$('#eUserLoginName').val(selectedRow.loginName);
	$('#ePwd').val(selectedRow.password);
	//$('#eUserPwd').val(selectedRow.password);
	$('#eUserDepart').val(selectedRow.departName);
	$('#eUserRoles').val(selectedRow.roleNames);
	$('#eUserSn').val(selectedRow.sn);
	$('#eUserSex').combobox('setValue', selectedRow.sex);
	$('#eUserPhone').val(selectedRow.phone);
	$('#eUserBirthday').datebox('setText', selectedRow.birthday);
	$('#eUserJobDate').val(selectedRow.jobDate);
	$('#eUserStatus').val(selectedRow.status);
	$('#eUserLocked').val(selectedRow.locked);
	$('#eUserPwd').focus();
	checkLoginName = selectedRow.loginName;
	$('#eUserWindow').window('open');	
}

function submitUser(){
	var selectedRow = $('#eUserDataGrid').datagrid('getSelected');
	var ePwd = $('#ePwd').val();
	var userPwd = $('#eUserPwd').val();
	var userSex = $('#eUserSex').combobox('getText');
	var userPhone = $('#eUserPhone').val();
	var userBirthday = $('#eUserBirthday').datebox('getText');
	
	var userLoginName = selectedRow.loginName;
	var userName = selectedRow.name;
	var userDepart = selectedRow.departId
	var strUserRoles = selectedRow.strRoleIds;
	var userSn = selectedRow.sn;
	var userJobDate = selectedRow.jobDate;
	var userStatus = selectedRow.status;
	var userLocked = selectedRow.locked;
	
	
	
	//如果隐藏密码为空
	if(ePwd == ''){
		//判断密码框为空  则提示错误
		if(userPwd==''){
			$.messager.alert('提示','密码不能为空!');
			return;
		}
	}
	
	if(userSex == ''){
		$.messager.alert('提示','请选择性别!');
		return;
	}

	
	//用户编辑
	$.post('userAjax!edit', {id:selectedRow.id, loginName:userLoginName, name:userName, password:userPwd, departId:userDepart, roles:strUserRoles,
		sn:userSn, sex:userSex, phone:userPhone, jobDate:userJobDate, birthday:userBirthday, status:userStatus, locked:userLocked},
		function(data){
			$("#eUserDataGrid").datagrid('reload'); 
		}, 'json');
	$('#eUserWindow').window('close');	
}


function clearTable(){
	$('#eUserLoginName').val('');
	$('#eUserName').val('');
	$('#eUserPwd').val('');
	$('#eUserDepart').val('');
	$('#eUserRoles').val('');
}

